﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace WCYCReader.SocketServer.Models
{
    public delegate void OnReceiveEventHandler(string xmlPack);

    ///<summary>   
    ///   MultiCast   的摘要说明。   
    ///</summary>   
    public class MultiCast
    {
        public static string IpAddress = "239.255.255.250";
        private static int Port = 50000;

        private string addr;
        private UdpClient udpClient;
        private bool bShutDown = true;
        private Thread threadReceive;

        ///<summary>   
        ///   数据到达事件   
        ///</summary>   
        public event OnReceiveEventHandler OnReceive;

        public MultiCast():this(IpAddress, Port)
        {
            
        }

        ///<summary>   
        ///   构造   
        ///</summary>   
        ///<param   name="multiCastIP">组播IP</param>   
        ///<param   name="udpPort">端口</param>   
        public MultiCast(string multiCastIP, int udpPort)
        {
            addr = multiCastIP;
            Port = udpPort;
            OnReceive += new OnReceiveEventHandler(MultiCast_OnReceive);
        }

        ///<summary>   
        ///   启动监听   
        ///</summary>   
        public void Startup()
        {
            if (!bShutDown)
                return;

            udpClient = new UdpClient(Port);
            IPAddress ipMultiCast = IPAddress.Parse(addr);

            try
            {
                udpClient.JoinMulticastGroup(ipMultiCast);
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("无法加入到多播组[" + addr + "]\n\n" + ex.ToString());
                return;
            }

            //抛出监听线程   
            bShutDown = false;
            threadReceive = new Thread(new ThreadStart(Receive));
            threadReceive.Start();
        }

        ///<summary>   
        ///   关闭监听   
        ///</summary>   
        public void ShutDown()
        {
            bShutDown = true;
            threadReceive.Join(500);

            try
            {
                threadReceive.Abort();
            }
            catch
            {

            }

            udpClient.Close();
            udpClient = null;
        }

        private void MultiCast_OnReceive(string xmlPack)
        {
            //do   nothing   
        }


        private void Receive()
        {
            byte[] bytes = null;
            IPEndPoint ipFrom = null;
            string xml = "";

            while (!bShutDown)
            {
                bytes = udpClient.Receive(ref   ipFrom);

                if (bytes != null)
                {
                    if (bytes.Length > 0)
                    {
                        xml = System.Text.ASCIIEncoding.ASCII.GetString(bytes);
                        //激发事件   
                        OnReceive(xml);
                    }
                }
            }
        }
    }
}